package com.easy.web.controller.ord;

import com.alibaba.fastjson.JSONObject;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.core.R;
import com.easy.common.core.domain.model.LoginBody;
import com.easy.common.utils.ServletUtils;
import com.easy.merchants.pojo.dto.HouseDto;
import com.easy.user.pojo.OrdFavorite;
import com.easy.user.service.IFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.concurrent.TimeUnit;

import static com.easy.common.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * 收藏 控制器
 *
 * @author RuoYu
 * @date 2022/11/26
 */
@RestController
@RequestMapping("/favorite")
@Api(tags = "收藏控制器")
public class CollectionController {

    private static final Logger log = LoggerFactory.getLogger(CollectionController.class);

    @Resource
    private IFavoriteService favoriteService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 判断当前登录用户是否收藏过该房屋
     *
     * @return {@link R}
     */
    @GetMapping("{rid}")
    @ApiOperation(value = "判断当前登录用户是否收藏过该房屋")
    public R isFavorite(@PathVariable Long rid) {
        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");

        Long uid = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "id") + "");

        boolean favorite = favoriteService.isFavorite(rid, uid);
        if (favorite) {
            return R.success("已收藏");
        }
        return R.error("未收藏");
    }

    /**
     * 添加收藏
     *
     * @return {@link R}
     */
    @PostMapping("{rid}/{title}")
    @ApiOperation(value = "添加收藏")
    public R addFavorite(@PathVariable Long rid, @PathVariable String title) {
        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");

        Long uid = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "id") + "");
        favoriteService.addFavorite(rid, uid, title);
        return R.success();
    }

    /**
     * 取消收藏
     *
     * @return {@link R}
     */
    @DeleteMapping("{rid}")
    @ApiOperation(value = "取消收藏")
    public R delFavorite(@PathVariable Long rid) {
        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");


        Long uid = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "id") + "");
        favoriteService.delFavorite(rid, uid);
        return R.success();
    }

    /**
     * 获取收藏列表
     *
     * @return {@link R}
     */
    @GetMapping("list")
    @ApiOperation(value = "获取收藏列表")
    public R getFavoriteList(int page, int pageSize, String title) {
        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        Long uid = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "id") + "");
        JSONObject favoriteList = favoriteService.getFavoriteList(page, pageSize, uid, title);
        return R.success(favoriteList);
    }

}
